package _01_动态数组;

public class _517_超级洗衣机 {


    public static void main(String[] args) {

        _517_超级洗衣机 v = new _517_超级洗衣机();
        int[] ins = new int[]{4, 0, 0, 4};
        System.out.println(v.findMinMoves(ins));
    }

    public int findMinMoves(int[] machines) {
        int total = 0;
        for (int machine : machines) {
            total += machine;
        }
        if (total % machines.length != 0) {
            return -1;
        }
        int avg = total / machines.length;
        int max = -1;
        int sum = 0;
        for (int machine : machines) {
            machine -= avg;
            sum += machine;
            max = Math.max(max, Math.max(Math.abs(sum), machine));
        }
        return max;
    }

}
